package com.sc.nft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.dao.PushCidRelationTagDao;
import com.sc.nft.dao.UserPushConfDao;
import com.sc.nft.dao.UserRelationPushCidDao;
import com.sc.nft.entity.PushCidRelationTag;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.UserPushConf;
import com.sc.nft.entity.UserRelationPushCid;
import com.sc.nft.enums.PushNoticeEnableEnum;
import com.sc.nft.enums.PushPlatformEnum;
import com.sc.nft.service.UserPushConfService;
import com.sc.nft.service.UserRelationPushCidService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author 刘国航
 * @desc
 * @since 2024/6/14 11:29
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class UserRelationPushCidServiceImpl implements UserRelationPushCidService {

    private final UserRelationPushCidDao userRelationPushCidDao;
    private final PushCidRelationTagDao pushCidRelationTagDao;
    private final UserPushConfDao userPushConfDao;
    private final UserPushConfService userPushConfService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bindCid(String cid, PushPlatformEnum platform, UserInfo userInfo) {
        //cid绑定的其他设备删掉
        List<UserRelationPushCid> userRelationPushCidList = userRelationPushCidDao.getByCid(cid);
        if (CollectionUtil.isNotEmpty(userRelationPushCidList)) {
            userRelationPushCidList.forEach(userRelationPushCid -> {
                if (!Objects.equals(userRelationPushCid.getUserId(), userInfo.getId())) {
                    userRelationPushCid.deleteById();
                }
            });
        }
        userRelationPushCidList = userRelationPushCidDao.getListByUserId(userInfo.getId());
        if (CollectionUtil.isNotEmpty(userRelationPushCidList)) {
            //用户id已经绑定过cid，重新绑定的时候，只保留一条，其他的删掉
            for (int i = 0; i < userRelationPushCidList.size(); i++) {
                UserRelationPushCid userRelationPushCid = userRelationPushCidList.get(i);
                if (Objects.equals(i, 0)) {
                    userRelationPushCid.setCid(cid);
                    userRelationPushCid.setPlatform(platform);
                    userRelationPushCid.setBindTime(DateTime.now());
                    userRelationPushCid.updateById();
                } else {
                    userRelationPushCid.deleteById();
                }
            }
        } else {
            UserRelationPushCid userRelationPushCid = new UserRelationPushCid();
            userRelationPushCid.setCid(cid);
            userRelationPushCid.setUserId(userInfo.getId());
            userRelationPushCid.setPlatform(platform);
            userRelationPushCid.setBindTime(DateTime.now());
            userRelationPushCid.insert();
        }
        Boolean enableNoticePush = true;
        UserPushConf userPushConf = userPushConfDao.getByUserId(userInfo.getId());
        if (Objects.nonNull(userPushConf)) {
            enableNoticePush = userPushConf.getEnableNoticePush();
        }
        PushCidRelationTag pushCidRelationTag = pushCidRelationTagDao.getByCid(cid);
        if (Objects.nonNull(pushCidRelationTag)) {
            if (enableNoticePush && !StrUtil.equals(pushCidRelationTag.getTag(), PushNoticeEnableEnum.ENABLE_NOTICE.getKey())) {
                userPushConfService.pushUserBindTag(userInfo.getId(), enableNoticePush, false);
            }
            if (!enableNoticePush && !StrUtil.equals(pushCidRelationTag.getTag(), PushNoticeEnableEnum.DISABLE_NOTICE.getKey())) {
                userPushConfService.pushUserBindTag(userInfo.getId(), enableNoticePush, false);
            }
        } else {
            userPushConfService.pushUserBindTag(userInfo.getId(), enableNoticePush, false);
        }
    }

}
